package com.ledim.service;

import android.content.Intent;
import com.ledim.app.LeDimApplication;
import com.ledim.bean.base.BaseResultInfo;
import com.ledim.activity.UserLoginActivity;
import com.ledim.utils.ToastUtil;
import rx.Observer;

/**
 * Created by ccx on 16/5/17.
 */
public class LedimHttpObserver<T extends BaseResultInfo> implements Observer<T> {

    @Override
    public void onCompleted() {

    }

    @Override
    public void onError(Throwable e) {
        e.printStackTrace();
    }

    @Override
    public void onNext(T t) {
        if (t != null) {
            if (!t.success) {
                if (t.code == ServiceCode.INVALID_SESSION) {
                    //session失效
                    ToastUtil.toast("授权过期，请重新登录");
                    Intent intent = new Intent(LeDimApplication.getInstance()
                                                               .getApplicationContext(), UserLoginActivity.class);
                    intent.putExtra(UserLoginActivity.NEED_LOGIN, true);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    LeDimApplication.getInstance().getApplicationContext().startActivity(intent);
                } else {
                    // ToastUtil.toast("服务器繁忙，请稍后重试!");
                }
            }
        } else {
            //TODO 服务器返回数据为null
            //ToastUtil.toast("服务器繁忙，请稍后重试!");
            return;
        }
    }
}
